# coding:utf-8

import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver
import json

from tornado.options import options
from tornado.web import RequestHandler

tornado.options.define("port", default=8000, type=int, help="run server on the given port")


class IndexHandler(RequestHandler):
    """请求处理类"""
    def post(self):
        body_arg = self.get_body_argument("a", "None")
        raw_body_arg = self.request.body
        content_type = self.request.headers["Content-Type"]
        json_args = json.loads(raw_body_arg)
        self.write("body_arg: %s<br/>" % body_arg)
        self.write("raw_body_arg: %s<br/>" % raw_body_arg)
        self.write("content_type: %s<br/>" % content_type)
        self.write("a: %s" % json_args.get("a"))

"""
/subject/bj/python
/subject/sh/c
/subject/bj/1103
"""


class SubjectCityHandler(RequestHandler):
    """"""
    def get(self, city, subject):
        self.write("city:%s<br/>" % city)
        self.write("subject:%s<br/>" % subject)


class SubjectDateHandler(RequestHandler):
    def get(self, date, city):
        self.write("date:%s<br/>" % date)
        self.write("city:%s<br/>" % city)



if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        [
            (r"/", IndexHandler),
            (r"/subject/(.+)/(.+)", SubjectCityHandler),
            (r"/subjectd/(?P<date>.+)/(?P<city>.+)", SubjectDateHandler)
        ],
        debug=True
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()